1 using UnityEngine;
2 using
System.Collections;
3 /*
4     Todo -
set duration and color properly (actually, i'm not sure this is possible)
5     calculate count based
on detail
6     inherit velocity
7 */
8
9 [RequireComponent (
typeof (Detonator))]
10 [AddComponentMenu(
"Detonator/Object Spray")]
11 public
class DetonatorSpray : DetonatorComponent {
12
13     
public GameObject sprayObject;
14     
public int count = 10;
15     
public float startingRadius = 0f;
16     
public float minScale = 1f;
17     
public float maxScale = 1f;
18     
19     
private bool _delayedExplosionStarted = false;
20     
private float _explodeDelay;
21     
22     
override public void Init()
23     {
24         
//unused
25     }
26
27     
void Update()
28     {
29         
if (_delayedExplosionStarted)
30         {
31             _explodeDelay = (_explodeDelay - Time.deltaTime);
32             
if (_explodeDelay <= 0f)
33             {
34                 Explode();
35             }
36         }
37     }
38     
39     
private Vector3 _explosionPosition;
40     
private float _tmpScale;
41     
override public void Explode()
42     {
43         
if (!_delayedExplosionStarted)
44         {
45             _explodeDelay = explodeDelayMin + (Random.
value * (explodeDelayMax - explodeDelayMin));
46         }
47         
if (_explodeDelay <= 0) //if the delayTime is zero
48         {
49             
int detailCount = (int)(detail * count);
50             
for (int i=0;i<detailCount;i++)
51             {
52                 Vector3 randVec = Random.onUnitSphere * (startingRadius * size);
53                 Vector3 velocityVec =
new Vector3((velocity.x*size),(velocity.y*size),(velocity.z*size));
54                 GameObject chunk = Instantiate(sprayObject, (
this.transform.position + randVec), this.transform.rotation) as GameObject;
55                 chunk.transform.parent =
this.transform;
56                 
57                 
//calculate scale for this piece
58                 _tmpScale = (minScale + (Random.
value * (maxScale - minScale)));
59                 _tmpScale = _tmpScale * size;
60
61                 chunk.transform.localScale =
new Vector3(_tmpScale,_tmpScale,_tmpScale);
62
63                 
if (MyDetonator().upwardsBias > 0f)
64                 {
65                     velocityVec =
new Vector3(
66                         (velocityVec.x / Mathf.Log(MyDetonator().upwardsBias)),
67                         (velocityVec.y * Mathf.Log(MyDetonator().upwardsBias)),
68                         (velocityVec.z / Mathf.Log(MyDetonator().upwardsBias))
69                         );
70                 }
71
72                 chunk.GetComponent<Rigidbody>().velocity = Vector3.Scale(randVec.normalized,velocityVec);
73                 chunk.GetComponent<Rigidbody>().velocity = Vector3.Scale(randVec.normalized,velocityVec);
74                 Destroy(chunk, (duration * timeScale));
75
76                 _delayedExplosionStarted =
false;
77                 _explodeDelay =
0f;
78             }
79         }
80         
else
81         {
82             
//tell update to start reducing the start delay and call explode again when it's zero
83             _delayedExplosionStarted =
true;
84         }
85     }
86     
87     
88     
89     
public void Reset()
90     {
91         velocity =
new Vector3(15f,15f,15f);
92     }
93 }


Gõ tìm kiếm nhanh...